package com.moli.iov.base.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.moli.iov.base.cache.redis.CityCacheService;
import com.moli.iov.base.cache.redis.SysParamSettingsCacheService;
import com.moli.iov.base.mapper.BaseCityMapper;
import com.moli.iov.base.model.dto.AddressDto;
import com.moli.iov.base.model.po.BaseCity;
import com.moli.iov.base.service.IBaseCityService;
import com.moli.iov.base.util.CollectionUtils;
import com.moli.iov.rest.RestResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 省市基础表 服务实现类
 * </p>
 *
 * @author lws
 * @since 2019-06-12
 */
@Service
public class BaseCityServiceImpl extends ServiceImpl<BaseCityMapper, BaseCity> implements
    IBaseCityService {

  @Autowired
  private CityCacheService cityCacheService;

  @Override
  public RestResponse<List<AddressDto>> getProvinceAndCity() {
    Map<String, AddressDto> provinceAndCity = cityCacheService.getProvinceAndCity();
    List<AddressDto>dtoList=new ArrayList<>();
     if (!CollectionUtils.isEmpty(provinceAndCity)){
       provinceAndCity.forEach((s, addressDto) -> dtoList.add(addressDto)
           );
     }
    return RestResponse.success(dtoList);
  }

  @Override
  public RestResponse<List<AddressDto>> queryAllAddressDtoTree() {
    Map<String, AddressDto> stringAddressDtoMap = cityCacheService.queryAllAddressDtoTree();
    List<AddressDto>dtoList=new ArrayList<>();
    if (!CollectionUtils.isEmpty(stringAddressDtoMap)){
      stringAddressDtoMap.forEach((s, addressDto) -> dtoList.add(addressDto)
      );
    }
    return RestResponse.success(dtoList);
  }

  @Override
  public void initCityCache() {
    cityCacheService.initCityCache();
  }
}
